<template>
  <div ref='ec' :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from "echarts";
export default {
  props: {
    width: {
      type: String,
      default: "0px"
    },
    height: {
      type: String,
      default: "0px"
    },
    data: {}
  },
  data() {
    return {
      chart: null
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.ec);
      this.chart.setOption(this.option);
    }
  },
  created() {
    const xData = (function() {
      const data = [];
      for (let i = 1; i < 8; i++) {
        data.push(i + "#车间");
      }
      return data;
    })();
    this.color = [
      "#0050E2",
      "#0780ED",
      "#00AFC3",
      "#44D428",
      "#FFC100",
      "#FF9200",
      "#EC0040",
      "#FF5227"
    ];
    this.option = {
      color: this.color,
      backgroundColor: "#fff",
      tooltip: {
        trigger: "axis",
        axisPointer: {
          textStyle: {
            color: "#fff"
          }
        }
      },
      grid: {
        // 网格位置 right
        right: 0,
        left: 0,
        // 网格底部距离
        bottom: 80
      },
      legend: {
        // 图例icon宽度
        itemWidth: 10,
        // 图例icon高度
        itemHeight: 10,
        // 图例位置  right
        left: 0,
        // 图例样式
        textStyle: {
          color: "#90979c",
          fontSize: "12"
        },
        data: [
          { name: "#1配电室", icon: "circle" },
          { name: "#2配电室", icon: "circle" },
          { name: "#3配电室", icon: "circle" },
          { name: "#4配电室", icon: "circle" },
          { name: "#5配电室", icon: "circle" },
          { name: "#6配电室", icon: "circle" },
          { name: "#7配电室", icon: "circle" },
        ]
      },
      calculable: true,
      xAxis: [
        {
          type: "category",
          axisLine: {
            lineStyle: {
              color: "#999"
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            formatter: function(value, index) {
              // let s = value.split("#")[0];
              // let s1 = value.split("#")[1];
              // let s2 = s1.split("").join("\n");
              return value.split("").join("\n");
              // return s + "#" + "\n" + s2;
            }
          },
          data: xData
        }
      ],
      yAxis: [
        {
          show: false
        }
      ],
      series: [
        {
          name: "#1配电室",
          type: "bar",
          stack: "total",
          barMaxWidth: 50,
          barGap: "10%",

          data: [709, 1917, 2455, 2610, 1719, 1433, 1544]
        },
        {
          name: "#2配电室",
          type: "bar",
          stack: "total",
          barMaxWidth: 50,
          barGap: "10%",

          data: [709, 1917, 2455, 2610, 1719, 1433, 1544]
        },
        {
          name: "#3配电室",
          type: "bar",
          stack: "total",
          barMaxWidth: 50,
          barGap: "10%",

          data: [709, 1917, 2455, 2610, 1719, 1433, 1544]
        },
        {
          name: "#4配电室",
          type: "bar",
          stack: "total",
          barMaxWidth: 50,
          barGap: "10%",

          data: [709, 1917, 2455, 2610, 1719, 1433, 1544]
        },
        {
          name: "#5配电室",
          type: "bar",
          stack: "total",
          barMaxWidth: 50,
          barGap: "10%",

          data: [709, 1917, 2455, 2610, 1719, 1433, 1544]
        },
        {
          name: "#6配电室",
          type: "bar",
          stack: "total",
          barMaxWidth: 50,
          barGap: "10%",

          data: [709, 1917, 2455, 2610, 1719, 1433, 1544]
        },
        {
          name: "#7配电室",
          type: "bar",
          stack: "total",
          barMaxWidth: 50,
          barGap: "10%",

          data: [709, 1917, 2455, 2610, 1719, 1433, 1544]
        },
        {
          name: "#8配电室",
          type: "bar",
          stack: "total",
          barMaxWidth: 50,
          barGap: "10%",

          data: [709, 1917, 2455, 2610, 1719, 1433, 1544]
        }
      ]
    };
  }
};
</script>
